package com.central.user.service.impl;

import com.central.annotation.datasource.DataSource;
import com.central.model.common.PageResult;
import com.central.model.common.Result;
import com.central.model.common.utils.HttpsUtil;
import com.central.model.common.utils.PageUtil;
import com.central.user.dao.PlayerDao;
import com.central.user.model.AccountInfo;
import com.central.user.model.BlackListInfo;
import com.central.user.model.ServerInfo;
import com.central.user.service.PlayerListServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PlayerListServiceImpl implements PlayerListServiceI {

    @Autowired
    private PlayerDao playerDao;

    private Result hallIpByPlayerId(Long playerId){
        Result result = null;
        try {
            int serverId = playerDao.getServerId(playerId);
            if (serverId == -1){
                return Result.failed(-1,"玩家不在线");
            }
            if (serverId == 0){
                return Result.failed(-1,"玩家状态异常");
            }
            ServerInfo serverInfo = playerDao.getAreaServerInfo(serverId);
            if (serverInfo.getIp().equals("")){
                return Result.failed(-1,"未获取到玩家对应大厅消息");
            }
            result = new Result();
            result.setDatas(serverInfo);
            result.setResp_code(1);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed(-1,"异常");
        }
    }

    @Override
    @DataSource(name = "log")
    public PageResult<BlackListInfo> getBlackListAll(Map<String, Object> params) {
        int total = 1;
        List<BlackListInfo> list = Collections.emptyList();
        if (!params.containsKey("playerId")){
            params.put("state",1);
            total = playerDao.getAccountCountByState();
        }
        PageUtil.pageParamConver(params, true);
        list = playerDao.getBlackListAll(params);

        return PageResult.<BlackListInfo>builder().data(list).code(0).count((long)total).build();
    }

    @Override
    @DataSource(name = "log")
    public PageResult<AccountInfo> getPlayerAll(Map<String, Object> params){
        int total = 1;
        List<AccountInfo> list = Collections.emptyList();
        if (params.containsKey("id")){
            PageUtil.pageParamConver(params, true);
            list = playerDao.getPlayerAll(params);
        }else {
            total = playerDao.getAccountCount();
            PageUtil.pageParamConver(params, true);
            list = playerDao.getPlayerAll(params);
        }
        return PageResult.<AccountInfo>builder().data(list).code(0).count((long)total).build();
    }

    //TODO   数据库链接 方法命名
    @Override
    public Result kickPlayer(String type,Long playerId){
        Result result = hallIpByPlayerId(playerId);
        ServerInfo serverInfo = null;
        if (result.getResp_code() != 1){
            return result;
        }else {
            serverInfo = (ServerInfo) result.getDatas();
        }
        String url = HttpsUtil.splicingUrl(serverInfo.getIp(),serverInfo.getPort(),type);
        Map<String,Object> map = new HashMap<>();
        map.put("playerIds", Arrays.asList(playerId));
        try {
            String str = HttpsUtil.post(url,new HashMap<>(),map,null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("踢出失败");
        }
        return Result.succeed("踢出成功");
    }

    @Override
    public Result addOrRemoveBlackList(String type, Long playerId, boolean freeze) {
        Result result = hallIpByPlayerId(playerId);
        ServerInfo serverInfo = null;
        if (result.getResp_code() != 1){
            return result;
        }else {
            serverInfo = (ServerInfo) result.getDatas();
        }
        String url = HttpsUtil.splicingUrl(serverInfo.getIp(),serverInfo.getPort(),type);
        Map<String,Object> map = new HashMap<>();
        map.put("playerIds", Arrays.asList(playerId));
        map.put("freeze",freeze);
        try {
            String str = HttpsUtil.post(url,new HashMap<>(),map,null);
            System.out.println(str);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed(freeze?"添加失败":"移除失败");
        }
        return Result.succeed(freeze?"添加成功":"移除成功");
    }
}
